package com.zhx.zaxiang.数据库的各种测试;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class InsertData {
    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/weight";
        String user = "root";
        String password = "wszhxo";

        try {
            Connection conn = DriverManager.getConnection(url, user, password);
            conn.setAutoCommit(false);

            String sql = "INSERT INTO mytable ( name, age) VALUES ( ?, ?)";
            PreparedStatement statement = conn.prepareStatement(sql);

            for (int i = 1; i <= 10000000; i++) {
                statement.setString(1, "Name" + i);
                statement.setInt(2, 20 + i % 10);
                statement.addBatch();

                if (i % 1000 == 0) {
                    statement.executeBatch();
                    conn.commit();
                    statement.clearBatch();
                }
            }

            statement.executeBatch();
            conn.commit();
            statement.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}